package com.lovo.ssc.controller;

import com.lovo.sscsys.dto.SellKillGoodsDto;
import com.lovo.ssc.entity.Seckill;
import com.lovo.ssc.entity.Shelves;
import com.lovo.ssc.service.ISeckillService;
import com.lovo.ssc.service.IShelvesService;
import com.lovo.util.MQUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-07
 */
@RestController
@RequestMapping("/seckill")
public class SeckillController {

    @Autowired
    IShelvesService shelvesService;

    @Autowired
    ISeckillService seckillService;

    @Autowired
    RabbitTemplate rabbitTemplate;

//    查询所有已上架的商品展示到后台前端促销维护页面
    @GetMapping("findAll")
    public List<Shelves> findAll(){
        return shelvesService.list();
    }


    //选择促销，修改售价，将促销实体传入促销审核队列
    @GetMapping("sellKillGoods/{goodsId}/{sellPrice}/{info}/discout")
    public void sellKillGoods(@PathVariable("goodsId") String id, @PathVariable("sellPrice") Float sellPrice,
                              @PathVariable("info") String info, @PathVariable("discount") Float discount){

        Shelves shelves = shelvesService.getById(id);
        //修改销售价
        shelves.setGoodspricce(sellPrice*discount);
        shelves.setGoodsstate("1");//0-已上架  1-促销中 2-预售商品

        shelvesService.save(shelves);

        SellKillGoodsDto dto = new SellKillGoodsDto();
        dto.setCode(shelves.getGoodscode());
        dto.setGoodsName(shelves.getGoodsname());
        dto.setInfo(info);
        dto.setGoodsId(Integer.parseInt(id));
        dto.setSellprice(shelves.getGoodspricce());
        Seckill seckill = new Seckill();

        BeanUtils.copyProperties(dto, seckill); //对象拷贝
        seckillService.save(seckill);
        rabbitTemplate.convertAndSend(MQUtil.sellKillCheackQueue,null,dto);



    }

    //取消促销，恢复售价，将实体传入取消促销审核队列
    @GetMapping("cancelSellKill/{goodsId}/{sellPrice}/{info}/discout")
    public void cancelSellKill(@PathVariable("goodsId") String id, @PathVariable("sellPrice") Float sellPrice,
                               @PathVariable("info") String info, @PathVariable("discount") Float discount) {

        Shelves shelves = shelvesService.getById(id);
        //修改销售价
        shelves.setGoodspricce(sellPrice / discount);
        shelves.setGoodsstate("0");//0-已上架  1-促销中 2-预售商品
//        shelves.setGoodsid(shelves.getGoodsid());
//        shelves.setGoodsbatch(shelves.getGoodsbatch());
//        shelves.setGoodsspecs(shelves.getGoodsspecs());
//        shelves.setNumber(shelves.getNumber());
//        shelves.setGoodstype(shelves.getGoodstype());
//        shelves.setGoodsname(shelves.getGoodsname());
//        shelves.setGoodscode(shelves.getGoodscode());
        shelvesService.save(shelves);

        SellKillGoodsDto dto = new SellKillGoodsDto();
        dto.setCode(shelves.getGoodscode());
        dto.setGoodsName(shelves.getGoodsname());
        dto.setInfo(info);
        dto.setGoodsId(Integer.parseInt(id));
        dto.setSellprice(shelves.getGoodspricce());
        Seckill seckill = new Seckill();

        BeanUtils.copyProperties(dto, seckill); //对象拷贝
        seckillService.save(seckill);
        rabbitTemplate.convertAndSend(MQUtil.cancelCheackQueue,null,dto);
    }

}
